
require(['./config'], () => {
    require(['header', 'footer', 'jquery', 'cbl'], () => {
        class Login {
            constructor() {
                this.init();
                this.user;
                this.login();
                this.register();
                this.number = 0;
                this.pwd = 0;
            }
            init() {
                this.click();
                this.setUser();
            }
            click() {
                $('.tit p').click(function () {
                    $(this).addClass('active1').siblings().removeClass('active1');
                    if ($(this).index() === 2) {
                        $('.ms').removeClass('hide').siblings().eq(1).addClass('hide');
                    } else {
                        $('.cons1').removeClass('hide').siblings('.ms').addClass('hide')
                    }
                })
                $('.cons1>.txt>a').eq(0).click(function () {
                    $('.tit p').eq(3).removeClass('hide').siblings().addClass('hide');
                    $('.cons3').removeClass('hide').siblings('.cons1').addClass('hide');
                })
                $('.ms>.txt>a').click(function () {
                    $('.tit p').eq(3).removeClass('hide').siblings().addClass('hide');
                    $('.cons3').removeClass('hide').siblings('.ms').addClass('hide');
                })
            }
            setUser() {
                this.user = localStorage.getItem('user')
                if (this.user) {
                } else {
                    localStorage.setItem('user', JSON.stringify([{ name: 'hxg123', pwd: 123456, status: 0 }]));
                }
            }
            login() {
                this.user = localStorage.getItem('user');
                const that = this;
                $(".cons1 p:nth-child(1) input").blur(function () {
                    that.number = $(this).val();
                })
                $(".cons1 p:nth-child(2) input").blur(function () {
                    that.pwd = $(this).val();
                })
                $(".cons1 p:nth-child(4) input").click(() => {
                    const user1 = JSON.parse(that.user);
                    const flag = user1.some((item, index) => {
                        return that.number === item.name;
                    })
                    if (!flag) {
                        alert("该账号不存在请前往注册");
                    } else {
                        const flag = user1.some((item, index) => {
                            return that.pwd == item.pwd && that.number === item.name;
                        })
                        if (!flag) {
                            alert("请输入正确的账号或密码");
                        } else {
                            user1.forEach(item => {
                                if (that.pwd == item.pwd && that.number === item.name) {
                                    item.status = 200;
                                    localStorage.setItem('user', JSON.stringify(user1));
                                    alert("登录成功");
                                    location.href = `/index.html`;
                                }
                            })
                        }
                    }
                })
            }
            register() {
                this.user = localStorage.getItem('user');
                var that = this;
                $(".resg .phone_num input").blur(function () {
                    const number = $(this).val();
                    that.number = number;
                })
                $(".resg .pwd_s input").blur(function () {
                    const pwd_s = $(this).val();
                    that.pwd = pwd_s;
                })
                $(".resg .sub").click(() => {
                    const user1 = JSON.parse(this.user);
                    const flag = user1.some((item, index) => {
                        return this.number == item.name;
                    })
                    if (!flag) {
                        user1.push({ name: this.number, pwd: this.pwd, status: 0 });
                        localStorage.setItem('user', JSON.stringify(user1));
                        location.href = '/html/login.html';
                    } else {
                        alert("该账号存在请另注册");
                    }
                })
            }
        }
        return new Login();
    })
})